<template>
  <div class="mine-container">
    <div class="container">
      <div class="mine">
        <div class="mine-switch" style="margin-top: 10px">
          <el-button type="primary" @click="init">我购买的</el-button>
          <el-button type="warning" @click="get_saleList">我卖出的</el-button>
        </div>
        <div class="mine-content">
          <!-- ---表格--------------------------------------------- -->
          <el-table
            :data="dataList"
            style="width: 890px">
            <el-table-column
              prop="id"
              label=""
              type=index>
            </el-table-column>
            <el-table-column
              label="图片">
                <template slot-scope="scope">
                  <el-image :src="scope.row.goodImg" width="60px" height="60px"></el-image>
                </template>
            </el-table-column>
            <el-table-column
              prop="goodName"
              label="商品名称">
            </el-table-column>
            <el-table-column
              prop="price"
              label="商品价格(￥)">
            </el-table-column>
            <el-table-column
              :prop="name"
              :label="label"
              >
            </el-table-column>
            <el-table-column
              :prop="time"
              :label="time_label"
              >
            </el-table-column>
            <el-table-column
              label="操作">
              <template slot-scope="scope">
                <el-popconfirm
                  title="确定退货吗"
                  @confirm="confirm(scope.row.id)"
                >
                  <el-button slot="reference">退货</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <!-- ------------------------------------------------ -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {getBuyList} from '@/api/mine/index.js'
import {getSaleList} from '@/api/mine/index.js'
import {delMineGood} from '@/api/mine/index.js'
export default {
  data() {
    return {
      dataList:[],
      name: '',
      label: '',
      time: 'createdAt',
      time_label:'购买时间'
    }
  },
  methods:{
    init() {
      let user = JSON.parse(localStorage.getItem('user')) 
      // console.log(user.username);
      let name = user.username
      this.name = 'sallerName'
      this.label = '卖家'
      this.time_label = '购买时间'
      getBuyList(name).then((res) => {
        // console.log(res.data);
        this.dataList = res.data.data
      })
    },

    get_saleList() {
      let user = JSON.parse(localStorage.getItem('user')) 
      // console.log(user.username);
      this.name = 'buyerName'
      this.label = '买家'
      this.time_label = '卖出时间'
      let name = user.username
      getSaleList(name).then(res => {
        // console.log(res.data);
        this.dataList = res.data.data
      })
    },
    confirm(id) {
      // console.log(id);
      delMineGood(id).then(()=>{
        // console.log(res.data);
        this.init()
      })
    },
  },
  created() {
    this.init()
  }
}
</script>

<style scoped>

</style>